﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections;
using System.Text;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for Report
/// 
/// Thao.Nguyen khuyến cáo
/// Trong lớp nào đó, không nên tính giá trị thông qua properties ngay trong lớp đó
/// Properties chỉ để tính toán, gọi từ các lớp khác
/// Tất cả tính toán ngay trong nội bộ lớp đó phải tính toán bởi các biến (Variables)
/// </summary>
namespace TTQT.TTQT_Code.Info
{
    public class Report
    {
        #region Consts
        private const string PARAM = "@Param";
        private const string PARAM_REPORTID = "@ReportID";
        private const string PARAM_REPORTNAME = "@ReportName";
        private const string PARAM_TENFILE = "@TenFile";
        private const string PARAM_TINHCHATAC = "@TinhChatAC";
        private const string PARAM_DOTPHATHANH = "@DotPhatHanh";
        private const string PARAM_LOAIANCHI = "@LoaiAnChi";
        private const string PARAM_TUNGAY = "@TuNgay";
        private const string PARAM_DENNGAY = "@DenNgay";
        private const string PARAM_KHODEN = "@KhoDen";
        private const string PARAM_CN = "@CN";
        private const string PARAM_PGD = "@PGD";
        private const string PARAM_GDV = "@GDV";
        private const string PARAM_SEP_CN = "@SEP_CN";
        private const string PARAM_KSV_QC_CN = "@KSV_QC_CN";
        private const string PARAM_KSV_QC_PGD = "@KSV_QC_PGD";
        private const string PARAM_GDV_PGD = "@GDV_PGD";

        #endregion

        #region Fields
        private string reportID;
        private string reportName;
        private string tenFile;
        private bool tinhChatAC;
        private bool dotPhatHanh;
        private bool loaiAnChi;
        private bool tuNgay;
        private bool denNgay;
        private bool khoDen;
        private bool cN;
        private bool pGD;
        private bool gDV;
        private bool sEP_CN;
        private bool kSV_QC_CN;
        private bool kSV_QC_PGD;
        private bool gDV_PGD; 
        #endregion

        #region Properties

        public string TenFile
        {
            get { return tenFile; }
            set { tenFile = value; }
        }

        public string ReportID
        {
            get { return reportID; }
            set { reportID = value; }
        }

        public string ReportName
        {
            get { return reportName; }
            set { reportName = value; }
        }

        public bool TinhChatAC
        {
            get { return tinhChatAC; }
            set { tinhChatAC = value; }
        }

        public bool DotPhatHanh
        {
            get { return dotPhatHanh; }
            set { dotPhatHanh = value; }
        }

        public bool LoaiAnChi
        {
            get { return loaiAnChi; }
            set { loaiAnChi = value; }
        }

        public bool TuNgay
        {
            get { return tuNgay; }
            set { tuNgay = value; }
        }

        public bool DenNgay
        {
            get { return denNgay; }
            set { denNgay = value; }
        }

        public bool KhoDen
        {
            get { return khoDen; }
            set { khoDen = value; }
        }

        public bool CN
        {
            get { return cN; }
            set { cN = value; }
        }

        public bool PGD
        {
            get { return pGD; }
            set { pGD = value; }
        }

        public bool GDV
        {
            get { return gDV; }
            set { gDV = value; }
        }

        public bool SEP_CN
        {
            get { return sEP_CN; }
            set { sEP_CN = value; }
        }

        public bool KSV_QC_CN
        {
            get { return kSV_QC_CN; }
            set { kSV_QC_CN = value; }
        }

        public bool KSV_QC_PGD
        {
            get { return kSV_QC_PGD; }
            set { kSV_QC_PGD = value; }
        }

        public bool GDV_PGD
        {
            get { return gDV_PGD; }
            set { gDV_PGD = value; }
        }

        #endregion

        #region Constructor
        public Report()
        {

            //
            // TODO: Add constructor logic here
            //
        }

        public Report(string reportID, string reportName, string tenFile, bool tinhChatAC, bool dotPhatHanh, bool loaiAnChi, bool tuNgay, bool denNgay, bool khoDen, bool cN, bool pGD, bool gDV, bool sEP_CN, bool kSV_QC_CN, bool kSV_QC_PGD, bool gDV_PGD)
        {
            this.reportID = reportID;
            this.reportName = reportName;
            this.tenFile = tenFile;
            this.tinhChatAC = tinhChatAC;
            this.dotPhatHanh = dotPhatHanh;
            this.loaiAnChi = loaiAnChi;
            this.tuNgay = tuNgay;
            this.denNgay = denNgay;
            this.khoDen = khoDen;
            this.cN = cN;
            this.pGD = pGD;
            this.gDV = gDV;
            this.sEP_CN = sEP_CN;
            this.kSV_QC_CN = kSV_QC_CN;
            this.kSV_QC_PGD = kSV_QC_PGD;
            this.gDV_PGD = gDV_PGD;
        }

        #endregion


        #region Methods

        public DataSet SelectAll(string err)
        {
            try
            {
                DataSet ds = new DataSet();
                SqlDataAdapter adt = new SqlDataAdapter("", "");
                adt.SelectCommand.CommandText = "SP_Reports";
                adt.SelectCommand.CommandType = CommandType.StoredProcedure;
                SqlParameter para = default(SqlParameter);
                para = adt.SelectCommand.Parameters.Add(PARAM, SqlDbType.NVarChar);
                para.Value = "selectall";
                ds = DAL.FillDataset(ref err, adt);
                return ds;
            }
            catch (Exception)
            {
                return  new DataSet();
            }

        }

        public DataTable selectByID(string err, string _reportID)
        {
            try
            {

                DataSet ds = new DataSet();
                SqlDataAdapter adt = new SqlDataAdapter("", "");
                adt.SelectCommand.CommandText = "SP_Reports";
                adt.SelectCommand.CommandType = CommandType.StoredProcedure;
                SqlParameter para = default(SqlParameter);
                para = adt.SelectCommand.Parameters.Add(PARAM, SqlDbType.NVarChar);
                para.Value = "selectByID";
                para = adt.SelectCommand.Parameters.Add(PARAM_REPORTID, SqlDbType.NVarChar);
                para.Value = _reportID;
                ds = DAL.FillDataset(ref err, adt);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    //Thao.Nguyen modified 20100804
                    //Sửa gán các biến thay vì gán properties
                    DataRow row = ds.Tables[0].Rows[0];
                    reportID = row["ReportID"].ToString();
                    reportName = row["ReportName"].ToString();
                    tenFile = row["TenFile"].ToString();
                    tinhChatAC = bool.Parse(row["TinhChatAC"].ToString());
                    dotPhatHanh = bool.Parse(row["DotPhatHanh"].ToString());
                    loaiAnChi = bool.Parse(row["LoaiAnChi"].ToString());
                    tuNgay = bool.Parse(row["TuNgay"].ToString());
                    denNgay = bool.Parse(row["DenNgay"].ToString());
                    khoDen = bool.Parse(row["KhoDen"].ToString());
                    cN = bool.Parse(row["CN"].ToString());
                    pGD = bool.Parse(row["PGD"].ToString());
                    gDV = bool.Parse(row["GDV"].ToString());
                    sEP_CN = bool.Parse(row["SEP_CN"].ToString());
                    kSV_QC_CN = bool.Parse(row["KSV_QC_CN"].ToString());
                    kSV_QC_PGD = bool.Parse(row["KSV_QC_PGD"].ToString());
                    gDV_PGD = bool.Parse(row["GDV_PGD"].ToString());
                }
                return ds.Tables[0];
            }
            catch (Exception)
            {
                return new DataTable();
            }

        }

        /// <summary>
        /// Lấy Báo cáo theo User đăng nhập
        /// </summary>
        /// <param name="_SEP_CN"></param>
        /// <param name="_KSV_QC_CN"></param>
        /// <param name="_KSV_QC_PGD"></param>
        /// <param name="_GDV_PGD"></param>
        /// <returns></returns>
        public DataTable GetbyUser(bool _SEP_CN, bool _KSV_QC_CN, bool _KSV_QC_PGD, bool _GDV_PGD)
        {
            try
            {
                string err = "";
                DataSet ds = new DataSet();
                SqlDataAdapter adt = new SqlDataAdapter("", "");
                adt.SelectCommand.CommandText = "SP_Reports";
                adt.SelectCommand.CommandType = CommandType.StoredProcedure;
                SqlParameter para = default(SqlParameter);
                para = adt.SelectCommand.Parameters.Add(PARAM, SqlDbType.NVarChar);
                para.Value = "GetbyUser";
                para = adt.SelectCommand.Parameters.Add(PARAM_SEP_CN, SqlDbType.Bit);
                para.Value = _SEP_CN;
                para = adt.SelectCommand.Parameters.Add(PARAM_KSV_QC_CN, SqlDbType.Bit);
                para.Value = _KSV_QC_CN;
                para = adt.SelectCommand.Parameters.Add(PARAM_KSV_QC_PGD, SqlDbType.Bit);
                para.Value = _KSV_QC_PGD;
                para = adt.SelectCommand.Parameters.Add(PARAM_GDV_PGD, SqlDbType.Bit);
                para.Value = _GDV_PGD;
                ds = DAL.FillDataset(ref err, adt);
                //if (ds.Tables[0].Rows.Count > 0)
                //{
                //    DataRow row = ds.Tables[0].Rows[0];
                //    ReportID = row["ReportID"].ToString();
                //    ReportName = row["ReportName"].ToString();
                //    TenFile = row["ReportName"].ToString();
                //    TinhChatAC = bool.Parse(row["TinhChatAC"].ToString());
                //    DotPhatHanh = bool.Parse(row["DotPhatHanh"].ToString());
                //    LoaiAnChi = bool.Parse(row["LoaiAnChi"].ToString());
                //    TuNgay = bool.Parse(row["TuNgay"].ToString());
                //    DenNgay = bool.Parse(row["DenNgay"].ToString());
                //    KhoDen = bool.Parse(row["KhoDen"].ToString());
                //    CN = bool.Parse(row["CN"].ToString());
                //    PGD = bool.Parse(row["PGD"].ToString());
                //    GDV = bool.Parse(row["GDV"].ToString());
                //    SEP_CN = bool.Parse(row["SEP_CN"].ToString());
                //    KSV_QC_CN = bool.Parse(row["KSV_QC_CN"].ToString());
                //    KSV_QC_PGD = bool.Parse(row["KSV_QC_PGD"].ToString());
                //    GDV_PGD = bool.Parse(row["GDV_PGD"].ToString());
                //}
                return ds.Tables[0];
            }
            catch (Exception)
            {
                return new DataTable();
            }
        }
        /// <summary>
        /// Lấy Báo cáo ấn chỉ mẫu theo User đăng nhập
        /// </summary>
        /// <param name="_SEP_CN"></param>
        /// <param name="_KSV_QC_CN"></param>
        /// <param name="_KSV_QC_PGD"></param>
        /// <param name="_GDV_PGD"></param>
        /// <returns></returns>
        public DataTable GetbyUser_Mau(bool _SEP_CN, bool _KSV_QC_CN, bool _KSV_QC_PGD, bool _GDV_PGD)
        {
            try
            {
                string err = "";
                DataSet ds = new DataSet();
                SqlDataAdapter adt = new SqlDataAdapter("", "");
                adt.SelectCommand.CommandText = "SP_Reports";
                adt.SelectCommand.CommandType = CommandType.StoredProcedure;
                SqlParameter para = default(SqlParameter);
                para = adt.SelectCommand.Parameters.Add(PARAM, SqlDbType.NVarChar);
                para.Value = "GetbyUser_Mau";
                para = adt.SelectCommand.Parameters.Add(PARAM_SEP_CN, SqlDbType.Bit);
                para.Value = _SEP_CN;
                para = adt.SelectCommand.Parameters.Add(PARAM_KSV_QC_CN, SqlDbType.Bit);
                para.Value = _KSV_QC_CN;
                para = adt.SelectCommand.Parameters.Add(PARAM_KSV_QC_PGD, SqlDbType.Bit);
                para.Value = _KSV_QC_PGD;
                para = adt.SelectCommand.Parameters.Add(PARAM_GDV_PGD, SqlDbType.Bit);
                para.Value = _GDV_PGD;
                ds = DAL.FillDataset(ref err, adt);
                //if (ds.Tables[0].Rows.Count > 0)
                //{
                //    DataRow row = ds.Tables[0].Rows[0];
                //    ReportID = row["ReportID"].ToString();
                //    ReportName = row["ReportName"].ToString();
                //    TenFile = row["ReportName"].ToString();
                //    TinhChatAC = bool.Parse(row["TinhChatAC"].ToString());
                //    DotPhatHanh = bool.Parse(row["DotPhatHanh"].ToString());
                //    LoaiAnChi = bool.Parse(row["LoaiAnChi"].ToString());
                //    TuNgay = bool.Parse(row["TuNgay"].ToString());
                //    DenNgay = bool.Parse(row["DenNgay"].ToString());
                //    KhoDen = bool.Parse(row["KhoDen"].ToString());
                //    CN = bool.Parse(row["CN"].ToString());
                //    PGD = bool.Parse(row["PGD"].ToString());
                //    GDV = bool.Parse(row["GDV"].ToString());
                //    SEP_CN = bool.Parse(row["SEP_CN"].ToString());
                //    KSV_QC_CN = bool.Parse(row["KSV_QC_CN"].ToString());
                //    KSV_QC_PGD = bool.Parse(row["KSV_QC_PGD"].ToString());
                //    GDV_PGD = bool.Parse(row["GDV_PGD"].ToString());
                //}
                return ds.Tables[0];
            }
            catch (Exception)
            {
                return new DataTable();
            }
        }
        public void Insert(string err)
        {
            Insert(err, reportID, reportName, tenFile, tinhChatAC, dotPhatHanh, loaiAnChi, tuNgay, denNgay, khoDen,cN, pGD,gDV_PGD, sEP_CN, kSV_QC_CN, kSV_QC_PGD, gDV_PGD);
        }

        public void Insert(string err, string reportid, string reportname, string tenfile, Boolean tinhchatac, Boolean dotphathanh, Boolean loaianchi, Boolean tungay, Boolean denngay, Boolean khoden, Boolean cn, Boolean pgd, Boolean gdv, Boolean sep_cn, Boolean ksv_qc_cn, Boolean ksv_qc_pgd, Boolean gdv_pgd)
        {
            try
            {

                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "SP_Reports";
                cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter para = default(SqlParameter);
                para = cmd.Parameters.Add(PARAM, SqlDbType.NVarChar);
                para.Value = "insert";
                para = cmd.Parameters.Add(PARAM_REPORTID, SqlDbType.VarChar);
                para.Value = reportid;
                para = cmd.Parameters.Add(PARAM_REPORTNAME, SqlDbType.NVarChar);
                para.Value = reportname;
                para = cmd.Parameters.Add(PARAM_TENFILE, SqlDbType.NVarChar);
                para.Value = tenfile;
                para = cmd.Parameters.Add(PARAM_TINHCHATAC, SqlDbType.Bit);
                para.Value = tinhchatac;
                para = cmd.Parameters.Add(PARAM_DOTPHATHANH, SqlDbType.Bit);
                para.Value = dotphathanh;
                para = cmd.Parameters.Add(PARAM_LOAIANCHI, SqlDbType.Bit);
                para.Value = loaianchi;
                para = cmd.Parameters.Add(PARAM_TUNGAY, SqlDbType.Bit);
                para.Value = tungay;
                para = cmd.Parameters.Add(PARAM_DENNGAY, SqlDbType.Bit);
                para.Value = denngay;
                para = cmd.Parameters.Add(PARAM_KHODEN, SqlDbType.Bit);
                para.Value = khoden;
                para = cmd.Parameters.Add(PARAM_CN, SqlDbType.Bit);
                para.Value = cn;
                para = cmd.Parameters.Add(PARAM_PGD, SqlDbType.Bit);
                para.Value = pgd;
                para = cmd.Parameters.Add(PARAM_GDV, SqlDbType.Bit);
                para.Value = gdv;
                para = cmd.Parameters.Add(PARAM_SEP_CN, SqlDbType.Bit);
                para.Value = sep_cn;
                para = cmd.Parameters.Add(PARAM_KSV_QC_CN, SqlDbType.Bit);
                para.Value = ksv_qc_cn;
                para = cmd.Parameters.Add(PARAM_KSV_QC_PGD, SqlDbType.Bit);
                para.Value = ksv_qc_pgd;
                para = cmd.Parameters.Add(PARAM_GDV_PGD, SqlDbType.Bit);
                para.Value = gdv_pgd;
                DAL.ExecCommand(ref err, cmd);
            }
            catch (Exception)
            {
                return;
            }
        }
        public void Update(string err)
        {
            Update(err, reportID, reportName, tenFile, tinhChatAC, dotPhatHanh, loaiAnChi, tuNgay, denNgay, khoDen, cN, pGD, gDV, sEP_CN, kSV_QC_CN, kSV_QC_PGD, gDV_PGD);
        }
        public void Update(string err, string reportid, string reportname, string tenfile, Boolean tinhchatac, Boolean dotphathanh, Boolean loaianchi, Boolean tungay, Boolean denngay, Boolean khoden, Boolean cn, Boolean pgd, Boolean gdv, Boolean sep_cn, Boolean ksv_qc_cn, Boolean ksv_qc_pgd, Boolean gdv_pgd)
        {
            try
            {

                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "SP_Reports";
                cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter para = default(SqlParameter);
                para = cmd.Parameters.Add(PARAM, SqlDbType.NVarChar);
                para.Value = "update";
                para = cmd.Parameters.Add(PARAM_REPORTID, SqlDbType.VarChar);
                para.Value = reportid;
                para = cmd.Parameters.Add(PARAM_REPORTNAME, SqlDbType.NVarChar);
                para.Value = reportname;
                para = cmd.Parameters.Add(PARAM_TENFILE, SqlDbType.NVarChar);
                para.Value = tenfile;
                para = cmd.Parameters.Add(PARAM_TINHCHATAC, SqlDbType.Bit);
                para.Value = tinhchatac;
                para = cmd.Parameters.Add(PARAM_DOTPHATHANH, SqlDbType.Bit);
                para.Value = dotphathanh;
                para = cmd.Parameters.Add(PARAM_LOAIANCHI, SqlDbType.Bit);
                para.Value = loaianchi;
                para = cmd.Parameters.Add(PARAM_TUNGAY, SqlDbType.Bit);
                para.Value = tungay;
                para = cmd.Parameters.Add(PARAM_DENNGAY, SqlDbType.Bit);
                para.Value = denngay;
                para = cmd.Parameters.Add(PARAM_KHODEN, SqlDbType.Bit);
                para.Value = khoden;
                para = cmd.Parameters.Add(PARAM_CN, SqlDbType.Bit);
                para.Value = cn;
                para = cmd.Parameters.Add(PARAM_PGD, SqlDbType.Bit);
                para.Value = pgd;
                para = cmd.Parameters.Add(PARAM_GDV, SqlDbType.Bit);
                para.Value = gdv;
                para = cmd.Parameters.Add(PARAM_SEP_CN, SqlDbType.Bit);
                para.Value = sep_cn;
                para = cmd.Parameters.Add(PARAM_KSV_QC_CN, SqlDbType.Bit);
                para.Value = ksv_qc_cn;
                para = cmd.Parameters.Add(PARAM_KSV_QC_PGD, SqlDbType.Bit);
                para.Value = ksv_qc_pgd;
                para = cmd.Parameters.Add(PARAM_GDV_PGD, SqlDbType.Bit);
                para.Value = gdv_pgd;
                DAL.ExecCommand(ref err, cmd);
            }
            catch (Exception)
            {
                return;
            }
        }

        public void Delete(string err)
        {
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "SP_Reports";
                cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter para = default(SqlParameter);
                para = cmd.Parameters.Add(PARAM, SqlDbType.NVarChar);
                para.Value = "delete";
                para = cmd.Parameters.Add(PARAM_REPORTID, SqlDbType.VarChar);
                para.Value = reportID;
                para = cmd.Parameters.Add(PARAM_REPORTNAME, SqlDbType.NVarChar);
                para.Value = reportName;
                para = cmd.Parameters.Add(PARAM_TENFILE, SqlDbType.NVarChar);
                para.Value = tenFile;
                para = cmd.Parameters.Add(PARAM_TINHCHATAC, SqlDbType.Bit);
                para.Value = tinhChatAC;
                para = cmd.Parameters.Add(PARAM_DOTPHATHANH, SqlDbType.Bit);
                para.Value = dotPhatHanh;
                para = cmd.Parameters.Add(PARAM_LOAIANCHI, SqlDbType.Bit);
                para.Value = loaiAnChi;
                para = cmd.Parameters.Add(PARAM_TUNGAY, SqlDbType.Bit);
                para.Value = tuNgay;
                para = cmd.Parameters.Add(PARAM_DENNGAY, SqlDbType.Bit);
                para.Value = denNgay;
                para = cmd.Parameters.Add(PARAM_KHODEN, SqlDbType.Bit);
                para.Value = khoDen;
                para = cmd.Parameters.Add(PARAM_CN, SqlDbType.Bit);
                para.Value = cN;
                para = cmd.Parameters.Add(PARAM_PGD, SqlDbType.Bit);
                para.Value = pGD;
                para = cmd.Parameters.Add(PARAM_GDV, SqlDbType.Bit);
                para.Value = gDV;
                para = cmd.Parameters.Add(PARAM_SEP_CN, SqlDbType.Bit);
                para.Value = sEP_CN;
                para = cmd.Parameters.Add(PARAM_KSV_QC_CN, SqlDbType.Bit);
                para.Value = kSV_QC_CN;
                para = cmd.Parameters.Add(PARAM_KSV_QC_PGD, SqlDbType.Bit);
                para.Value = kSV_QC_PGD;
                para = cmd.Parameters.Add(PARAM_GDV_PGD, SqlDbType.Bit);
                para.Value = gDV_PGD;
                DAL.ExecCommand(ref err, cmd);
            }
            catch (Exception ex)
            {
                return;
            }
        }

        

        #endregion

    }
}




